package cn.com.wangluotongxin.merchants.service.impl;

import cn.com.wangluotongxin.common.exceptions.BadResponseException;
import cn.com.wangluotongxin.common.pojo.TenantUser;
import cn.com.wangluotongxin.merchants.dto.GoodsStandardDto;
import cn.com.wangluotongxin.merchants.form.GoodsStandardEditForm;
import cn.com.wangluotongxin.merchants.form.GoodsStandardForm;
import cn.com.wangluotongxin.merchants.model.MerchantsGoodsInfoMx;
import cn.com.wangluotongxin.merchants.model.MerchantsGoodsStandard;
import cn.com.wangluotongxin.merchants.mapper.MerchantsGoodsStandardMapper;
import cn.com.wangluotongxin.merchants.model.constants.GoodsInfoMxAuditStatus;
import cn.com.wangluotongxin.merchants.service.IMerchantsGoodsInfoMxService;
import cn.com.wangluotongxin.merchants.service.IMerchantsGoodsStandardColorService;
import cn.com.wangluotongxin.merchants.service.IMerchantsGoodsStandardService;
import cn.com.wangluotongxin.merchants.service.IMerchantsGoodsStandardSizeService;
import cn.com.wangluotongxin.merchants.vo.GoodsStandardVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商户管理-商品管理-商品规格 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-06-20
 */
@Service
public class MerchantsGoodsStandardServiceImpl extends ServiceImpl<MerchantsGoodsStandardMapper, MerchantsGoodsStandard> implements IMerchantsGoodsStandardService {

    @Autowired
    private IMerchantsGoodsInfoMxService goodsInfoMxService;

    @Override
    public IPage<GoodsStandardVo> getPage(GoodsStandardDto dto) {
        Page page = Page.of(dto.getPageNumber(), dto.getPageSize());
        return baseMapper.getPage(page, dto);
    }

    @Override
    public GoodsStandardVo detail(Integer id) {
        return baseMapper.detail(id);
    }

    @Override
    public void add(GoodsStandardForm form, TenantUser tenantUser) {
        if(this.count(new LambdaQueryWrapper<MerchantsGoodsStandard>()
                .eq(MerchantsGoodsStandard::getStandardName, form.getStandardName())) > 0) {
            throw new BadResponseException("规格名称【"+ form.getStandardName() +"】已存在");
        }

        MerchantsGoodsStandard goodsStandard = new MerchantsGoodsStandard();
        BeanUtils.copyProperties(form, goodsStandard);
        goodsStandard.setCreateUser(tenantUser.getId());
        goodsStandard.setCreateName(tenantUser.getUsername());
        this.save(goodsStandard);

        // 同步商品明细
        goodsInfoMxService.syncRecord(goodsStandard.getId(), goodsStandard.getGoodsId());
    }

    @Override
    public void edit(GoodsStandardEditForm form, TenantUser tenantUser) {
        if(this.count(new LambdaQueryWrapper<MerchantsGoodsStandard>()
                .ne(MerchantsGoodsStandard::getId, form.getId())
                .eq(MerchantsGoodsStandard::getStandardName, form.getStandardName())) > 0) {
            throw new BadResponseException("规格名称【"+ form.getStandardName() +"】已存在");
        }

        MerchantsGoodsInfoMx goodsInfoMx = goodsInfoMxService.getByStandard(form.getId());
        if(goodsInfoMx != null && GoodsInfoMxAuditStatus.AUDIT.getCode().equals(goodsInfoMx.getAuditStatus())) {
            throw new BadResponseException("商品正在审核中，请撤回审核后更改信息");
        }

        MerchantsGoodsStandard goodsStandard = new MerchantsGoodsStandard();
        BeanUtils.copyProperties(form, goodsStandard);
        goodsStandard.setModifyUser(tenantUser.getId());
        goodsStandard.setModifyName(tenantUser.getUsername());
        this.updateById(goodsStandard);

        // 同步商品明细
        goodsInfoMxService.syncRecord(goodsStandard.getId(), goodsStandard.getGoodsId());
    }

    @Override
    public void sale(Integer id, Integer isSale, TenantUser tenantUser) {
        this.update(new LambdaUpdateWrapper<MerchantsGoodsStandard>()
                .set(MerchantsGoodsStandard::getIsSale, isSale)
                .set(MerchantsGoodsStandard::getModifyUser, tenantUser.getId())
                .set(MerchantsGoodsStandard::getModifyName, tenantUser.getUsername())
                .eq(MerchantsGoodsStandard::getId, id));
    }
}
